import apis from '../../api/apis.js';
export default {
	namespaced: true,
	state: {
		//保存用户相关信息
		userInfo: {},
		userId: "",
		// 我的钱包
		myWallet: {
			balance: 0.00,
			redPacket: "8",
			discountCoupon: "5"
		},
		customerAddress: [],
		defaultAddress: {}
	},
	mutations: {
		//获取用户收货地址信息
		setUserAddress(state, newData) {
			state.customerAddress = newData;
			state.defaultAddress = newData.filter(item => item.isdefault == 1)[0];
			uni.setStorageSync("defaultAddress",state.defaultAddress);

		},
		// 获取用户相关信息
		setUserInfo(state, userInfo) {
			state.userInfo = userInfo;
			state.userId = userInfo.id;
		},
		// 设置用户Id
		setUserId(state, newId) {
			state.userId = newId;
		},
		// 充值后设置余额
		setBalance(state, addAoumt) {
			state.myWallet.balance = addAoumt - 0 + state.myWallet.balance;
		}
	},
	actions: {
		//获取验证码
		async getusercode(context, phoneNum) {
			let data = await apis.users.getUserCode({
				phoneNum
			});
			console.log(data);
		},
		// 手机号和验证码登录
		async loginByTel(context, userInfor) {
			let data = await apis.users.loginbyTelephone(userInfor);
			return data;
		},
		//注册
		async userRegister(context, data) {
			const res = await apis.users.register(data);
			context.commit(" setUserInfo", res.custom);
			return res
		},
		//获取省份
		async myGetAllProvince(context) {
			const res = await apis.users.getAllProvince();
			// console.log(res);
			return res
		},
		// 获取市区
		async myGetAllCityByPid(context, pid) {
			const res = await apis.users.getAllCityByPid({
				pid
			});
			return res
		},
		//获取地区
		async myGetAllZoneByCid(context, cid) {
			const res = await apis.users.getAllZoneByCid({
				cid
			});
			return res
		},
		//查看用户所有收货地址
		async mylookAddress(context) {
			const res = await apis.users.lookAddress({
				userId: context.state.userInfo.id
			});
			context.commit("setUserAddress", res.list);
		},
		//添加用户地址
		async myAddAddress(context, data) {
			const res = await apis.users.addAddress({
				userId: context.state.userInfo.id,
				...data
			});
			return res
		},
		//设置用户默认收货地址
		async mySetDefaultAddress(context, id) {
			const res = await apis.users.setDefaultAddress({
				id
			});
			return res;
			context.dispatch("mylookAddress");
		},
		async userDelAddress(context, id) {
			const res = await apis.users.delAddress({
				id
			})
		},
		// 账号密码登录
		async loginbyPassword(context, userInfor) {
			let data = await apis.users.loginBypassword(userInfor);
			console.log(data)
			return data;
		},
		// 获取用户个人信息 
		async userInformation(context) {
			let data = await apis.users.getUserPersonalInfo({
				id: context.state.userId
			});
			return data;
		},
		//删除用户收货地址
		async delUserAddress(context, id) {
			const res = await apis.users.delAddress({
				id
			});
			return res
		},
		//修改用户收货地址
		async myUpdataAddress(context, data) {
			const res = await apis.users.updataAddress({
				userId: context.state.userInfo.id,
				...data
			});
			return res
		}
	}
}
